package com.wuyou.simple;

import java.util.Stack;

/**
 * IsValid
 *
 * @Date 2024/12/17 17:15
 * @Author 无忧
 */
public class IsValid {

    public static void main(String[] args) {
        IsValid isValid = new IsValid();
        String s = "(]";

        System.out.println(isValid.isValid(s));
        System.out.println((int) '(');
        System.out.println((int) ')');
        System.out.println((int) '[');
        System.out.println((int) ']');
        System.out.println((int) '{');
        System.out.println((int) '}');
    }

    public boolean isValid(String s) {
        char[] chars = s.toCharArray();
        char c1 = '(', c2 = ')', c3 = '[', c4 = ']', c5 = '{', c6 = '}';
        Stack<Character> stack = new Stack<Character>();
        for(int i=0;i<chars.length;i++){
            char t = chars[i];
            if(t == c1 || t == c3 || t == c5){
                stack.push(t);
            }else {
                Character pop = stack.pop();
                if(t == c2 && pop != c1){
                    return false;
                }
                if(t == c4 && pop != c3){
                    return false;
                }
                if(t == c6 && pop != c5){
                    return false;
                }
            }
        }
        return stack.size() == 0;
    }
}
